import React from 'react';
import Item, { IFormItemProps } from './Item'
import { Cascader } from 'antd';
import { CascaderProps, CascaderOptionType } from 'antd/lib/cascader';

import CityData from 'constants/cityData'

// 递归方法往数组里面插值
function changeCityData(arr: any) {
	let newArr = arr.map((item: any) => {
		let { label } = item;
		item.value = label

		if (item.children) {
			item.children = changeCityData(item.children)
		}
		return item
	})
	return newArr

}
changeCityData(CityData);

interface ICascaderProps extends CascaderProps, IFormItemProps {
	type?: 'area' | 'city',

}

// console.log("CityData:", CityData)
export default class FormCascader extends React.Component<ICascaderProps, any> {
	state = {
		cityData: CityData,
	}

	render() {
		const { cityData } = this.state;

		let { type, options } = this.props

		return (
			<Item {...this.props}
				render={() => (
					<Cascader options={type === "area" ? cityData : options} />
				)}
			/>

		)
	}

}